package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.PayApp;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.PayGroupLogBo;
import org.dromara.system.domain.vo.PayGroupLogVo;
import org.dromara.system.domain.PayGroupLog;
import org.dromara.system.mapper.PayGroupLogMapper;
import org.dromara.system.service.IPayGroupLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 轮询日志Service业务层处理
 *
 * @author Gavino
 * @date 2025-05-06
 */
@RequiredArgsConstructor
@Service
public class PayGroupLogServiceImpl implements IPayGroupLogService {

    private final PayGroupLogMapper baseMapper;

    /**
     * 查询轮询日志
     *
     * @param logId 主键
     * @return 轮询日志
     */
    @Override
    public PayGroupLogVo queryById(Long logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 分页查询轮询日志列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 轮询日志分页列表
     */
    @Override
    public TableDataInfo<PayGroupLogVo> queryPageList(PayGroupLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayGroupLog> lqw = buildQueryWrapper(bo);
        Page<PayGroupLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的轮询日志列表
     *
     * @param bo 查询条件
     * @return 轮询日志列表
     */
    @Override
    public List<PayGroupLogVo> queryList(PayGroupLogBo bo) {
        LambdaQueryWrapper<PayGroupLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayGroupLog> buildQueryWrapper(PayGroupLogBo bo) {
        Map<String, Object> params = bo.getParams();
        bo.setStatus(2);
        LambdaQueryWrapper<PayGroupLog> lqw = Wrappers.lambdaQuery();

        if (LoginHelper.isManager()) {
            lqw.eq(PayGroupLog::getManagerId, LoginHelper.getUserId());
        }
        else if (LoginHelper.isAgent()) {
            lqw.eq(PayGroupLog::getAgentId, LoginHelper.getUserId());
        }
        else if (!LoginHelper.isSuperAdmin()) {
            lqw.eq(PayGroupLog::getClientId, LoginHelper.getUserId());
        }

        lqw.eq(bo.getAppId() != null, PayGroupLog::getAppId, bo.getAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperate()), PayGroupLog::getOperate, bo.getOperate());
        lqw.like(StringUtils.isNotBlank(bo.getParamsLog()), PayGroupLog::getParamsLog, bo.getParamsLog());
        lqw.like(StringUtils.isNotBlank(bo.getReturnLog()), PayGroupLog::getReturnLog, bo.getReturnLog());
        lqw.eq(bo.getStatus() != null, PayGroupLog::getStatus, bo.getStatus());
        lqw.eq(bo.getGroupId() != null, PayGroupLog::getGroupId, bo.getGroupId());
        lqw.eq(bo.getClientId() != null, PayGroupLog::getClientId, bo.getClientId());
        lqw.eq(bo.getAgentId() != null, PayGroupLog::getAgentId, bo.getAgentId());
        lqw.eq(bo.getManagerId() != null, PayGroupLog::getManagerId, bo.getManagerId());

        lqw.orderByDesc(PayGroupLog::getLogId);
        return lqw;
    }

    /**
     * 新增轮询日志
     *
     * @param bo 轮询日志
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(PayGroupLogBo bo) {
        PayGroupLog add = MapstructUtils.convert(bo, PayGroupLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改轮询日志
     *
     * @param bo 轮询日志
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(PayGroupLogBo bo) {
        PayGroupLog update = MapstructUtils.convert(bo, PayGroupLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PayGroupLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除轮询日志信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        LambdaQueryWrapper<PayGroupLog> query = new LambdaQueryWrapper<>();
        query.in(PayGroupLog::getLogId, ids);
        if (LoginHelper.isManager()) {
            query.eq(PayGroupLog::getManagerId, LoginHelper.getUserId());
        }
        else if (LoginHelper.isAgent()) {
            query.eq(PayGroupLog::getAgentId, LoginHelper.getUserId());
        }
        else if (!LoginHelper.isSuperAdmin()) {
            query.eq(PayGroupLog::getClientId, LoginHelper.getUserId());
        }
        return baseMapper.delete(query) > 0;
    }
}
